<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title></title>
  </head>
  <body>
    <script>
      const arr = [1, 1, 1, 17, 8, 3, 3, 17, 2];
      // 利用Set
      const res1 = Array.from(new Set(arr));
      console.log(res1);
      // 两层for循环+splice
      const arr2 = [1, 1, 1, 17, 8, 3, 3, 17, 2];
      const unique1 = (arr) => {
        let len = arr.length;
        for (let i = 0; i < len; i++) {
          for (let j = i + 1; j < len; j++) {
            if (arr[i] === arr[j]) {
              arr.splice(j, 1);
              len--;
              j--;
            }
          }
        }
        return arr;
      };
      // 利用indexOf || include
      // indexOf
      const unique2 = (arr) => {
        const res = [];
        for (let i = 0; i < arr.length; i++) {
          if (res.indexOf(arr[i]) === -1) res.push(arr[i]);
        }
        return res;
      };
      // include
      const unique3 = (arr) => {
        const res = [];
        for (let i = 0; i < arr.length; i++) {
          if (!res.includes(arr[i])) res.push(arr[i]);
        }
        return res;
      };
      // 利用Map
      const unique4 = (arr) => {
        const map = new Map();
        const res = [];
        for (let i = 0; i < arr.length; i++) {
          if (!map.has(arr[i])) {
            map.set(arr[i], true);
            res.push(arr[i]);
          }
        }
        return res;
      };
    </script>
  </body>
</html>
